package com.cskaoyan.service;

import com.cskaoyan.bean.User;
import com.cskaoyan.bean.UserData;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author stone
 * @date 2022/01/06 16:36
 */
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    UserMapper userMapper;

    @Override
    public UserData query(BaseParam param, String username) {
        Integer page = param.getPage();//页码
        Integer limit = param.getLimit();//数据量
        String sort = param.getSort();//排序列
        String order = param.getOrder();//desc\asc
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        PageHelper.startPage(page, limit);
        //执行查询过程中拼接分页信息
        List<User> list = userMapper.select(sort,order,username);

        //total、pages、list
        //通过PageHelper帮我们来获得一些和分页相关的信息PageInfo
        //使用其有参构造方法 → 形参写来源于数据库的list
        PageInfo<User> pageInfo = new PageInfo<>(list);
        //list.size() !一定= total   → total ≥ list.size()
        long total = pageInfo.getTotal();//拿到满足条件的总的数据量（不考虑分页）
        int pages = pageInfo.getPages();
        return UserData.data(total,pages,limit,page,list);
    }
}
